"use strict";

const Controller = require("egg").Controller;
const jwt = require("jsonwebtoken");
const Axios = require("axios");
const client_id =
  "3400cccebf48fae463f54a342e0c8a6b3c158a2f44081c6b07f1a767b7875f3c";
const client_secret =
  "bb1a663996368a79cc124af22538cedae86eaaeb47bec94334fec1f5fb3909ca";
const redirect_uri = "http://127.0.0.1:7001/api/oauth/redirect";
//引入验证码
const SvgCaptcha = require("svg-captcha");
class HomeController extends Controller {
  // 第三方登录
  async auth() {
    const { ctx, app } = this;
    const { code } = ctx.query;
    const response = await Axios.post(
      `https://gitee.com/oauth/token?grant_type=authorization_code&code=${code}&client_id=${client_id}&redirect_uri=${redirect_uri}&client_secret=${client_secret}`
    );
    console.log(response);
    ctx.response.redirect(`http://localhost:8080/home`);
  }
  //查找数据
  async GETMock() {
    const { ctx, app } = this;
    const data = await app.mysql.select("data");
    ctx.body = {
      code: 200,
      message: "发送成功",
      data: data,
    };
  }
  //登录数据
  async LoginMock() {
    const { ctx, app } = this;
    const data = await app.mysql.select("Login");
    ctx.body = {
      code: 200,
      message: "发送成功",
      data: data,
    };
  }
  //登录
  async Login() {
    const { ctx, app } = this;
    const { phone, password } = ctx.request.body;
    const data = await app.mysql.get("login", {
      phone,
      password,
    });
    if (!data) {
      ctx.body = {
        code: 200,
        message: "请注册",
      };
    } else {
      const token = jwt.sign({ ...data, password: "" }, "1904A", {
        expiresIn: "24",
      });
      ctx.body = {
        code: 200,
        message: "登录成功",
        token,
      };
    }
  }
  //注册
  async Register() {
    const { ctx, app } = this;
    const { phone, nickname, password = "", avatar, email } = ctx.request.body;
    const data = await app.mysql.insert("login", {
      phone,
      nickname,
      password,
      avatar,
      email,
    });
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "注册成功",
      };
    } else {
      ctx.body = {
        code: 404,
        message: "注册失败",
      };
    }
  }
  //验证码
  async getCode() {
    const { ctx } = this;
    const options = {
      size: 4,
      fontSize: 50,
      width: 100,
      height: 30,
      color: true,
    };
    const cap = SvgCaptcha.create(options);
    ctx.session.code = cap.text;
    ctx.body = {
      code: 200,
      message: "验证码获取成功",
      data: cap.data,
    };
  }
  //增删改查数据
  async BlogsMock() {
    let { ctx, app } = this;
    let data = await app.mysql.select("blogs");

    ctx.body = {
      code: 200,
      message: "增删改查数据",
      data: data,
    };
  }
  //删除
  async DelMock() {
    const { ctx, app } = this;
    const { id } = ctx.params;
    const data = await app.mysql.query(`delete from blogs where id=?`, [id]);
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "删除成功",
      };
    } else {
      ctx.body = {
        code: 404,
        message: "删除失败",
      };
    }
  }
  //搜索
  async Search() {
    const { ctx, app } = this;
    const { val } = ctx.request.body;
    const ipt = `select * from blogs where 1=1 and title like "%${val}%"`;
    const list = await app.mysql.query(ipt);
    ctx.body = {
      list,
    };
  }
  //搜索
  async MOCKSearch() {
    const { ctx, app } = this;
    const { val } = ctx.request.body;
    const ipt = `select * from data where 1=1 and title like "%${val}%"`;
    const list = await app.mysql.query(ipt);
    ctx.body = {
      list,
    };
  }
  //添加保存
  async AddMock() {
    const { ctx, app } = this;
    const { userId, title, context } = ctx.request.body;
    const data = await app.mysql.query(
      `insert into blogs (userId,title,context) values (?,?,?)`,
      [userId, title, context]
    );
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "添加成功",
        data,
      };
    } else {
      ctx.body = {
        code: 404,
        message: "添加失败",
      };
    }
  }

  //创建数据
  async SETMock() {
    const { ctx, app } = this;
    const { title } = ctx.request.body;
    const data = await app.mysql.query(
      `insert into addmock (title) values (?)`,
      [title]
    );
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "保存成功",
        data,
      };
    } else {
      ctx.body = {
        code: 404,
        message: "保存失败",
      };
    }
  }
  //添加Tab标签数据
  async InertMock() {
    let { ctx, app } = this;
    let data = await app.mysql.select("addMock");

    ctx.body = {
      code: 200,
      message: "保存成功",
      data: data,
    };
  }
  //删除数据
  async DelSMock() {
    const { ctx, app } = this;
    const { id } = ctx.params;
    const data = await app.mysql.query(`delete from addmock where id=?`, [id]);
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "删除成功",
      };
    } else {
      ctx.body = {
        code: 404,
        message: "删除失败",
      };
    }
  }
  //OSS数据
  async OSSImgMock() {
    const { ctx, app } = this;
    const data = await app.mysql.select("img");
    ctx.body = {
      code: 200,
      message: "图片数据成功",
      data: data,
    };
  }
  //访问统计数据
  async VisitMockData() {
    const { ctx, app } = this;
    const data = await app.mysql.select("visit");
    ctx.body = {
      code: 200,
      message: "访问数据",
      data: data,
    };
  }
  //删除数据
  async VisitDelMock() {
    const { ctx, app } = this;
    const { id } = ctx.params;
    const data = await app.mysql.query(`delete from visit where id=?`, [id]);
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "删除成功",
      };
    } else {
      ctx.body = {
        code: 404,
        message: "删除失败",
      };
    }
  }
  //搜索
  async VisitSearch() {
    const { ctx, app } = this;
    const { val } = ctx.request.body;
    const ipt = `select * from visit where 1=1 and PageView like "%${val}%"`;
    const list = await app.mysql.query(ipt);
    ctx.body = {
      list,
    };
  }
  //保存标题
  async labelMock() {
    const { ctx, app } = this;
    const { title,id } = ctx.request.body;
    const data = await app.mysql.query(
      `insert into saer (id,title) values (?,?)`,
      [id,title]
    );
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "保存成功",
        data,
      };
    } else {
      ctx.body = {
        code: 404,
        message: "保存失败",
      };
    }
  }
  async labelFunc(){
    const {ctx,app} = this;
    const data = await app.mysql.select("saer");
    ctx.body = {
      code:200,
      message:"添加成功",
      data:data
    }
  }
  //删除数据
  async SaErDelMock() {
    const { ctx, app } = this;
    const { id } = ctx.params;
    const data = await app.mysql.query(`delete from saer where id=?`, [id]);
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "删除成功",
      };
    } else {
      ctx.body = {
        code: 404,
        message: "删除失败",
      };
    }
  }
}

module.exports = HomeController;
